home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 1995 #5 & #6 / Amiga Plus CD - 1995 - No. 5 and 6.iso / tex / macros / distribs / verbatim / verbatim.doc < prev    next >
Text File  |  1992-01-19  |  58KB  |  1,493 lines

  1. \def\fileversion{v1.4i}
  2. \def\filedate{92/01/20}
  3. \def\docdate{91/12/18}
  4.  
  5. %% \CheckSum{446}
  6. %% \CharacterTable
  7. %%  {Upper-case    \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
  8. %%   Lower-case    \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
  9. %%   Digits        \0\1\2\3\4\5\6\7\8\9
  10. %%   Exclamation   \!     Double quote  \"     Hash (number) \#
  11. %%   Dollar        \$     Percent       \%     Ampersand     \&
  12. %%   Acute accent  \'     Left paren    \(     Right paren   \)
  13. %%   Asterisk      \*     Plus          \+     Comma         \,
  14. %%   Minus         \-     Point         \.     Solidus       \/
  15. %%   Colon         \:     Semicolon     \;     Less than     \<
  16. %%   Equals        \=     Greater than  \>     Question mark \?
  17. %%   Commercial at \@     Left bracket  \[     Backslash     \\
  18. %%   Right bracket \]     Circumflex    \^     Underscore    \_
  19. %%   Grave accent  \`     Left brace    \{     Vertical bar  \|
  20. %%   Right brace   \}     Tilde         \~}
  21. %%
  22. %
  23. %\iffalse   % this is a METACOMMENT !
  24. %
  25. %
  26. %% Style-option `verbatim' to use with LaTeX v2.09
  27. %% Copyright (C) 1989, 1990, 1991 by Rainer Sch\"opf, all rights reserved.
  28. %
  29. % Copying of this file is authorized only if either
  30. % (1) you make absolutely no changes to your copy, including name, or
  31. % (2) if you do make changes, you name it something other than
  32. %     verbatim.doc.
  33. % This restriction helps ensure that all styles developed here
  34. % remain identical.
  35. %
  36. %
  37. % Error reports please to: R. Sch\"opf
  38. %                          Konrad-Zuse-Zentrum
  39. %                          f\"ur Informationstechnik Berlin
  40. %                          Heilbronner Str.\ 10
  41. %                          D-1000 Berlin 31
  42. %                          Federal Republic of Germany
  43. %                  Email:  <SCHOEPF@SC.ZIB-Berlin.DE>
  44. %
  45. %
  46. %\fi
  47. %
  48. % \changes{v1.4g}{91/11/21}{Several improvements in the documentation.}
  49. % \changes{v1.4f}{91/08/05}{Corrected bug in documentation.
  50. %                           Found by Bernd Raichle.}
  51. % \changes{v1.4e}{91/07/24}{Avoid reading this file twice.}
  52. % \changes{v1.4d}{91/04/24}{`penalty`interlinepenalty added to
  53. %                           definition of `par in `@verbatim.
  54. %                           Necessary to avoid page breaks in
  55. %                           the scope of a `samepage declaration.}
  56. % \changes{v1.4c}{90/10/18}{Added `leavevmode to definition of
  57. %       backquote macro.}
  58. % \changes{v1.4b}{90/07/14}{Converted nearly all `verb's to \|...\|.}
  59. % \changes{v1.4a}{90/04/04}{Added a number of percent characters
  60. %       to suppress blank space at the end of some code lines.}
  61. % \changes{v1.4}{90/03/07}{`verb rewritten.}
  62. %
  63. % \changes{v1.3a}{90/02/04}{Removed {\tt verbatimwrite} environment
  64. %       from the code. Now only shown as an example.}
  65. %
  66. % \changes{v1.2g}{90/02/01}{Revised documentation.}
  67. % \changes{v1.2e}{90/01/15}{Added `every@verbatim hook.}
  68. % \changes{v1.2d}{89/11/29}{Use token register `@temptokena instead
  69. %                           of macro `@tempb.}
  70. % \changes{v1.2d}{89/11/29}{Use token register `verbatim@line instead
  71. %                           of macro `@tempd.}
  72. % \changes{v1.2b}{89/10/25}{`verbatimfile renamed to `verbatiminput.
  73. %                           Suggested by Reinhard Wonneberger.}
  74. %
  75. % \changes{v1.1}{89/10/09}{Made the code more modular (as suggested by
  76. %                          Chris Rowley):  introduced
  77. %                          `verbatim@addtoline, etc.  Added
  78. %                          `verbatimwrite environment.}
  79. %
  80. % \changes{v1.0e}{89/07/17}{Fixed bug in `verbatimfile (*-form
  81. %         handling, discovered by Dirk Kreimer).}
  82. % \changes{v1.0d}{89/05/16}{Revised documentation, fixed silly bug
  83. %         in `verbatim@@@.}
  84. % \changes{v1.0c}{89/05/12}{Added redefinition of `@sverb, change
  85. %         in end-of-line handling.}
  86. % \changes{v1.0b}{89/05/09}{Change in `verbatim@rescan.}
  87. % \changes{v1.0a}{89/05/07}{Change in `verbatim@@testend.}
  88. %
  89. %
  90. % \DoNotIndex{\ ,\!,\C,\[,\\,\],\^,\`,\{,\},\~}
  91. % \DoNotIndex{\@M,\@empty,\@flushglue,\@gobble,\@ifstar,\@ifundefined}
  92. % \DoNotIndex{\@namedef,\@spaces,\@tempa,\@tempb,\@tempc,\@tempd}
  93. % \DoNotIndex{\@temptokena,\@totalleftmargin,\@warning,\active}
  94. % \DoNotIndex{\aftergroup,\begingroup,\catcode,\char,\csname,\def,\do}
  95. % \DoNotIndex{\docdate,\dospecials,\edef,\else,\endcsname,\endgroup}
  96. % \DoNotIndex{\endtrivlist,\expandafter,\fi,\filedate,\fileversion}
  97. % \DoNotIndex{\frenchspacing,\futurelet,\gdef,\if,\ifcat}
  98. % \DoNotIndex{\ifx,\immediate,\item,\kern,\lccode,\leftskip,\let}
  99. % \DoNotIndex{\lowercase,\next,\noexpand,\parfillskip,\parindent}
  100. % \DoNotIndex{\parskip,\penalty,\relax,\rightskip,\space,\string,\the}
  101. % \DoNotIndex{\toks@,\trivlist,\tt,\typeout,\vskip,\write,\z@}
  102. %
  103. %
  104. % \title{A New Implementation of \LaTeX{}'s \\ {\tt verbatim}
  105. %        and {\tt verbatim*} Environments\thanks{%
  106. %        This file has version number \fileversion{} dated \filedate.
  107. %        The documentation was last revised on \docdate.
  108. %       }}
  109. %
  110. % \author{Rainer Sch\"opf$\,$\thanks{ Many thanks to Chris Rowley
  111. %           from The Open University, UK, for looking this over,
  112. %           making a lot of useful suggestions, and discovering bugs.
  113. %           And many thanks to all the beta testers who tried this
  114. %           style file out.}\\
  115. %         Konrad-Zuse-Zentrum\\
  116. %         f\"ur Informationstechnik Berlin\\
  117. %         Heilbronner Str.\ 10\\
  118. %         W-1000 Berlin 31\\
  119. %         Federal Republic of Germany\\
  120. %         Internet: {\tt SCHOEPF@SC.ZIB-Berlin.DE}}
  121. %
  122. %
  123. % \date{August 5, 1991}
  124. %
  125. % \markboth{Verbatim style option, version \fileversion, as of \filedate}
  126. %          {Verbatim style option, version \fileversion, as of \filedate}
  127. %
  128. % \maketitle
  129. %
  130. % \begin{abstract}
  131. %   This style option reimplements the \LaTeX{} {\tt verbatim} and
  132. %   {\tt verbatim*} environments.
  133. %   In addition it provides a {\tt comment} environment
  134. %   that skips any commands or text between
  135. %   |\begin{comment}|
  136. %   and the next |\end{comment}|.
  137. %   It also contains a redefinition of \LaTeX's |\verb| command
  138. %   to better detect the omission of the closing delimiter.
  139. % \end{abstract}
  140. %
  141. % \section{Usage notes}
  142. %
  143. % \let\docDescribeMacro\DescribeMacro
  144. % \let\docDescribeEnv\DescribeEnv
  145. % \def\DescribeMacro#1{}
  146. % \def\DescribeEnv#1{}
  147. % \LaTeX's {\tt verbatim} and {\tt verbatim*} environments
  148. % have a few features that may give rise to problems. These are:
  149. % \begin{itemize}
  150. %   \item
  151. %     Since \TeX{} has to read all the text between the
  152. %     |\begin{verbatim}| and the |\end{verbatim}| before it can output
  153. %     anything, long verbatim listings may overflow \TeX's memory.
  154. %   \item
  155. %     Due to the method used to detect the closing |\end{verbatim}|
  156. %     (i.e.\ macro parameter delimiting) you cannot leave spaces
  157. %     between the |\end| token and |{verbatim}|.
  158. % \end{itemize}
  159. % Whereas the last of these points can be considered
  160. % only a minor nuisance the other one is a real limitation.
  161. %
  162. %
  163. % \DescribeEnv{verbatim}
  164. % \DescribeEnv{verbatim*}
  165. % This style file contains a reimplementation of the {\tt verbatim}
  166. % and {\tt verbatim*} environments which overcomes these restrictions.
  167. % There is, however, one incompatibility between the old and the
  168. % new implementations of these environments: the old version
  169. % would treat text on the same line as the |\end{verbatim}|
  170. % command as if it were on a line by itself.
  171. % {\bf This new version will simply ignore it.}\footnote{This is
  172. %   the price one has to pay for the removal of the old
  173. %   {\tt verbatim} environment's size limitations.}
  174. % It will, however, issue a warning message of the form
  175. % \begin{verbatim}
  176. %LaTeX warning: Characters dropped
  177. %               after \end{verbatim*}!
  178. %\end{verbatim}
  179. % This is not a real problem since this text can easily be put
  180. % on the next line without affecting the output.
  181. %
  182. % This new implementation also solves the second problem mentioned
  183. % above: it is possible to leave spaces (but {\em not\/} end of line)
  184. % between the |\end| and the |{verbatim}| or |{verbatim*}|:
  185. % \begin{verbatim}
  186. %\begin {verbatim*}
  187. %   test
  188. %   test
  189. %\end {verbatim*}
  190. %\end{verbatim}
  191. %
  192. % \DescribeEnv{comment}
  193. % Additionally we introduce a {\tt comment} environment, with the
  194. % effect that the text between |\begin{comment}| and |\end{comment}|
  195. % is simply ignored, regardless of what it looks like.
  196. % At first sight this seems to be quite different from the purpose
  197. % of verbatim listing, but actually these two concepts turn out to be
  198. % very similar.
  199. % Both rely on the fact that the text between |\begin{...}| and
  200. % |\end{...}| is read by \TeX{} without interpreting any commands or
  201. % special characters.
  202. % The remaining difference between {\tt verbatim} and {\tt comment} is
  203. % only that the text is to be typeset in the former case and to be
  204. % thrown away in the latter.
  205. %
  206. % \DescribeMacro{\verbatiminput}
  207. % |\verbatiminput| is a command with one argument that inputs a file
  208. % verbatim, i.e.\ the command |verbatiminput{xx.yy}|
  209. % has the same effect as\\[2pt]
  210. %   \hspace*{\MacroIndent}|\begin{verbatim}|\\
  211. %   \hspace*{\MacroIndent}\meta{Contents of the file {\tt xx.yy}}\\
  212. %   \hspace*{\MacroIndent}|\end{verbatim}|\\[2pt]
  213. % This command has also a |*|-variant that prints spaces as \verb*+ +.
  214. %
  215. %
  216. % \StopEventually{}
  217. %
  218. %
  219. % \section{Interfaces for style file designers}
  220. %
  221. % The {\tt verbatim} environment of \LaTeX{} version 2.09 does not
  222. % offer a good interface to programmers.
  223. % In contrast, this style file provides a simple mechanism to
  224. % implement similar features, the {\tt comment} environment provided
  225. % here being an example of what can be done and how.
  226. %
  227. %
  228. % \subsection{Simple examples}
  229. %
  230. % It is now possible to use the {\tt verbatim} environment to define
  231. % environments of your own.
  232. % E.g.,
  233. %\begin{verbatim}
  234. % \newenvironment{myverbatim}%
  235. %   {\endgraf\noindent MYVERBATIM:%
  236. %    \endgraf\verbatim}%
  237. %   {\endverbatim}
  238. %\end{verbatim}
  239. % can be used afterwards like the {\tt verbatim} environment, i.e.
  240. % \begin{verbatim}
  241. %\begin {myverbatim}
  242. %  test
  243. %  test
  244. %\end {myverbatim}
  245. %\end{verbatim}
  246. % Another way to use it is to write
  247. % \begin{verbatim}
  248. %\let\foo=\comment
  249. %\let\endfoo=\endcomment
  250. %\end{verbatim}
  251. % and from that point on environment {\tt foo} is the same as the
  252. % comment environment, i.e.\ everything inside its body is ignored.
  253. %
  254. % You may also add special commands after the |\verbatim| macro is
  255. % invoked, e.g.
  256. %\begin{verbatim}
  257. %\newenvironment{myverbatim}%
  258. %  {\verbatim\myspecialverbatimsetup}%
  259. %  {\endverbatim}
  260. %\end{verbatim}
  261. % though you may want to learn about the hook |\every@verbatim| at
  262. % this point.
  263. % However, there are still a number of restrictions:
  264. % \begin{enumerate}
  265. %   \item
  266. %     You must not use |\begin{verbatim}| inside a definition, e.g.
  267. %     \begin{verbatim*}
  268. %\newenvironment{myverbatim}%
  269. %{\endgraf\noindent MYVERBATIM:%
  270. % \endgraf\begin{verbatim}}%
  271. %{\end{verbatim}}
  272. %\end{verbatim*}
  273. %     If you try this example, \TeX{} will report a
  274. %     ``runaway argument'' error.
  275. %     More generally, it is not possible to use
  276. %     |\begin{verbatim}|\ldots\allowbreak|\end{verbatim}|
  277. %     or the related environments in the definition of the new
  278. %     environment.
  279. %   \item
  280. %     You can{\em not\/} use the {\tt verbatim} environment inside
  281. %     user defined {\em commands}; e.g.,
  282. % \changes{v1.4g}{91/11/21}{Corrcted wrong position of optional
  283. %        argument to `newcommand. Discovered by Piet van Oostrum.}
  284. %     \begin{verbatim*}
  285. %\newcommand{\verbatimfile}[1]%
  286. %{\begin{verbatim}%
  287. % \input{#1}%
  288. % \end{verbatim}}
  289. %\end{verbatim*}
  290. %     does {\em not\/} work; nor does
  291. %     \begin{verbatim}
  292. %\newcommand{\verbatimfile}[1]%
  293. %  {\verbatim\input{#1}\endverbatim}
  294. %\end{verbatim}
  295. %   \item The name of the newly defined environment must not contain
  296. %     characters with category code other than $11$ (letter) or
  297. %     $12$ (other), or this will not work.
  298. % \end{enumerate}
  299. %
  300. %
  301. % \subsection{The interfaces}
  302. %
  303. % \DescribeMacro{\verbatim@font}
  304. % Let us start with the simple things.
  305. % Sometimes it may be necessary to use a special typeface for your
  306. % verbatim text, or perhaps the usual computer modern typewriter shape
  307. % in a reduced size.
  308. %
  309. % You may select this by redefining the macro |\verbatim@font|.
  310. % This macro is executed at the beginning of every verbatim text to
  311. % select the font shape.
  312. % Do not use it for other purposes; if you find yourself abusing this
  313. % you may want to read about the |\every@verbatim| hook below.
  314. %
  315. % By default, |\verbatim@font| switches to the typewriter font and
  316. % disables the |?`| and |!`| ligatures.
  317. %
  318. %
  319. % \DescribeMacro{\every@verbatim}
  320. % \DescribeMacro{\addto@hook}
  321. % There is a hook (i.e.\ a token register) called |\every@verbatim|
  322. % whose contents are inserted into \TeX's mouth just before every
  323. % verbatim text.
  324. % Please use the |\addto@hook| macro to add something to this hook.
  325. % It is used as follows:\\[2pt]
  326. % \hspace*{\MacroIndent}|\addto@hook|\meta{name of the hook}\\
  327. % \hspace*{\MacroIndent}\hspace*{\MacroIndent}^^A
  328. %  |{|\meta{commands to be added}|}|
  329. % \vspace*{2pt}
  330. %
  331. %
  332. %
  333. % \DescribeMacro{\verbatim@start}
  334. % After all specific setup, like switching of category codes, has been
  335. % done, the |\verbatim@start| 
  336. % \newpage
  337. % \onecolumn
  338. % \begin{multicols}{2}
  339. % \noindent
  340. % macro is called.
  341. % This starts the main loop of the scanning mechanism implemented here.
  342. % Any other environment that wants to make use of this feature should
  343. % call this macro as its last action.
  344. %
  345. %
  346. % \DescribeMacro{\verbatim@startline}
  347. % \DescribeMacro{\verbatim@addtoline}
  348. % \DescribeMacro{\verbatim@processline}
  349. % \DescribeMacro{\verbatim@finish}
  350. % These are the things that concern the start of a verbatim
  351. % environment.
  352. % Once this (and other) setup has been done, the code in this style
  353. % file reads and processes characters from the input stream in the
  354. % following way:
  355. % \begin{enumerate}
  356. %   \item Before it starts to read the first character of an input line
  357. %     the macro |\verbatim@startline| is called.
  358. %   \item After some characters have been read, the macro
  359. %     |\verbatim@addtoline| is called with these characters as its only
  360. %     argument.
  361. %     This may happen several times per line (when an |\end| command is
  362. %     present on the line in question).
  363. %   \item When the end of the line is reached, the macro
  364. %     |\verbatim@processline| is called to process the characters that
  365. %     |\verbatim@addtoline| has accumulated.
  366. %   \item Finally, there is the macro |\verbatim@finish| that is called
  367. %     just before the environment is ended by a call to the |\end|
  368. %      macro.
  369. % \end{enumerate}
  370. %
  371. %
  372. % To make this clear consider the standard {\tt verbatim} environment.
  373. % In this case the three macros above are defined as follows:
  374. % \begin{enumerate}
  375. %   \item |\verbatim@startline| clears the character buffer
  376. %     (a token register).
  377. %   \item |\verbatim@addtoline| adds its argument to the character
  378. %     buffer.
  379. %   \item |\verbatim@processline| typesets the characters accumulated
  380. %     in the buffer.
  381. % \end{enumerate}
  382. % With this it is very simple to implement the {\tt comment}
  383. % environment:
  384. % in this case |\verbatim@startline| and |\verbatim@processline| are
  385. % no-ops whereas |\verbatim@addtoline| discards its argument.
  386. %
  387. %
  388. % Another possibility is to define a variant of the |verbatim|
  389. % environment that prints line numbers in the left margin.
  390. % Assume that this would be done by a counter called |VerbatimLineNo|.
  391. % Assuming that this counter was initialized properly by the
  392. % environment, |\verbatim@processline| would be defined in this case as
  393. % \begin{verbatim}
  394. %\def\verbatim@processline{%
  395. %  \addtocounter{VerbatimLineNo}{1}%
  396. %  \leavevmode
  397. %  \llap{\theVerbatimLineNo
  398. %        \ \hskip\@totalleftmargin}%
  399. %  \the\verbatim@line\par}
  400. %\end{verbatim}
  401. %
  402. % As a final nontrivial example we describe the definition of an
  403. % environment called {\tt verbatimwrite}.
  404. % It writes all text in its body to a file the name of which it is
  405. % given as an argument.
  406. % We assume that a stream number called |\verbatim@out| has already
  407. % been reserved by means of the |\newwrite| macro.
  408. %
  409. % Let's begin with the definition of the macro |\verbatimwrite|.
  410. % \begin{verbatim}
  411. %\def\verbatimwrite#1{%
  412. %\end{verbatim}
  413. % First we call |\@bsphack| so that this environment does not influence
  414. % the spacing.
  415. % Then we open the file and set the category codes of all special
  416. % characters:
  417. % \begin{verbatim}
  418. %  \@bsphack
  419. %  \immediate\openout \verbatim@out #1
  420. %  \let\do\@makeother\dospecials
  421. %  \catcode`\^^M\active
  422. %\end{verbatim}
  423. % The default definitions of the macros
  424. % \begin{verbatim}
  425. %  \verbatim@startline
  426. %  \verbatim@addtoline
  427. %  \verbatim@finish
  428. %\end{verbatim}
  429. % are also used in this environment.
  430. % Only the macro |\verbatim@processline| has to be changed before
  431. % |\verbatim@start| is called:
  432. % \begin{verbatim}
  433. %  \def\verbatim@processline{%
  434. %    \immediate\write\verbatim@out
  435. %      {\the\verbatim@line}}%
  436. %  \verbatim@start}
  437. %\end{verbatim}
  438. % The definition of |\endverbatimwrite| is very simple:
  439. % we close the stream and call |\@esphack| to get the spacing right.
  440. % \begin{verbatim}
  441. %\def\endverbatimwrite{%
  442. %  \immediate\closeout\verbatim@out
  443. %  \@esphack}
  444. %\end{verbatim}
  445. % \end{multicols}
  446. %
  447. % \newpage
  448. % \SelfDocumenting
  449. %
  450. % \section{The implementation}
  451. %
  452. % \let\DescribeMacro\docDescribeMacro
  453. % \let\DescribeEnv\docDescribeEnv
  454. %
  455. % \changes{v1.4e}{91/07/24}{Avoid reading this file twice.}
  456. % The very first thing we do is to ensure that this file is not read
  457. % in twice. To this end we check whether the macro |\verbatim@@@| is
  458. % defined. If so, we just stop reading this file.
  459. %    \begin{macrocode}
  460. \@ifundefined{verbatim@@@}{}{\endinput}
  461. %    \end{macrocode}
  462. %
  463. % We use a mechanism similar to the one implemented for the
  464. % |\comment|\ldots\allowbreak|\endcomment| macro in \AmSTeX:
  465. % We input one line at a time and check if it contains the |\end{...}|
  466. % tokens.
  467. % Then we can decide whether we have reached the end of the verbatim
  468. % text, or must continue.
  469. %
  470. % As always we begin by identifying the latest version of this file on
  471. % the VDU and in the transcript file.
  472. %    \begin{macrocode}
  473. \typeout{Style-Option: `verbatim'
  474.    \fileversion \space <\filedate>  (RmS)}
  475. \typeout{English Documentation
  476.    \@spaces \@spaces \space <\docdate>  (RmS)}
  477. %    \end{macrocode}
  478. %
  479. %
  480. % \subsection{Preliminaries}
  481. %
  482. % \begin{macro}{\addto@hook}
  483. % \changes{v1.2e}{90/01/15}{`addto@hook added.}
  484. %    We begin by defining a macro that adds tokens to a hook.
  485. %    The first argument is supposed to be a token register, the
  486. %    second consists of arbitrary \TeX{} text.
  487. %    \begin{macrocode}
  488. \def\addto@hook#1#2{#1\expandafter{\the#1#2}}
  489. %    \end{macrocode}
  490. % \end{macro}
  491. %
  492. % \begin{macro}{\every@verbatim}
  493. % \changes{v1.2e}{90/01/15}{`every@verbatim hook added.}
  494. %    The hook (i.e.\ token register) |\every@verbatim|
  495. %    is initialized to \meta{empty}.
  496. %    \begin{macrocode}
  497. \newtoks\every@verbatim
  498. \every@verbatim={}
  499. %    \end{macrocode}
  500. % \end{macro}
  501. %
  502. %
  503. % \begin{macro}{\@makeother}
  504. % \changes{v1.1a}{89/10/16}{`@makeother added.}
  505. %    |\@makeother| takes as argument a character and changes
  506. %    its category code to $12$ (other).
  507. %    \begin{macrocode}
  508. \def\@makeother#1{\catcode`#112\relax}
  509. %    \end{macrocode}
  510. % \end{macro}
  511. %
  512. %
  513. % \begin{macro}{\@vobeyspaces}
  514. % \changes{v1.1a}{89/10/16}{`@vobeyspaces added.}
  515. %    The macro |\@vobeyspaces| causes spaces in the input
  516. %    to be printed as spaces in the output.
  517. %    \begin{macrocode}
  518. \begingroup
  519.  \catcode`\ =\active%
  520. \gdef\@vobeyspaces{\catcode`\ \active\let \@xobeysp}%
  521. \endgroup
  522. %    \end{macrocode}
  523. % \end{macro}
  524. %
  525. %
  526. % \begin{macro}{\@xobeysp}
  527. % \changes{v1.1a}{89/10/16}{`@xobeysp added.}
  528. %    The macro |\@xobeysp| produces exactly one space in
  529. %    the output, protected against breaking just before it.
  530. %    (|\@M| is an abbreviation for the number $10000$.)
  531. %    \begin{macrocode}
  532. \def\@xobeysp{\leavevmode\penalty\@M\ }
  533. %    \end{macrocode}
  534. % \end{macro}
  535. %
  536. %
  537. % \begin{macro}{\verbatim@line}
  538. % \changes{v1.2d}{89/11/29}{Introduced token register `verbatim@line.}
  539. %    We use a newly defined token register called |\verbatim@line|
  540. %    that will be used as the character buffer.
  541. %    \begin{macrocode}
  542. \newtoks\verbatim@line
  543. %    \end{macrocode}
  544. % \end{macro}
  545. %
  546. % The following four macros are defined globally in a way suitable for
  547. % the {\tt verbatim} and {\tt verbatim*} environments.
  548. % \begin{macro}{\verbatim@startline}
  549. % \begin{macro}{\verbatim@addtoline}
  550. % \begin{macro}{\verbatim@processline}
  551. %    |\verbatim@startline| initializes processing of a line
  552. %    by emptying the character buffer (|\verbatim@line|).
  553. %    \begin{macrocode}
  554. \def\verbatim@startline{\verbatim@line{}}
  555. %    \end{macrocode}
  556. %    |\verbatim@addtoline| adds the tokens in its argument
  557. %    to our buffer register |\verbatim@line| without expanding
  558. %    them.
  559. %    \begin{macrocode}
  560. \def\verbatim@addtoline#1{%
  561.   \verbatim@line\expandafter{\the\verbatim@line#1}}
  562. %    \end{macrocode}
  563. %    Processing a line inside a {\tt verbatim} or {\tt verbatim*}
  564. %    environment means printing it.
  565. % \changes{v1.2c}{89/10/31}{Changed `@@par to `par in
  566. %    `verbatim@processline.  Removed `leavevmode and `null
  567. %    (i.e.\ the empty `hbox).}
  568. %    Ending the line means that we have to begin a new paragraph.
  569. %    We use |\par| for this purpose.  Note that |\par|
  570. %    is redefined in |\@verbatim| to force \TeX{} into horizontal
  571. %    mode and to insert an empty box so that empty lines in the input
  572. %    do appear in the output.
  573. % \changes{v1.2f}{90/01/31}{Removed `verbatim@startline.}
  574. %    \begin{macrocode}
  575. \def\verbatim@processline{\the\verbatim@line\par}
  576. %    \end{macrocode}
  577. % \end{macro}
  578. % \end{macro}
  579. % \end{macro}
  580. %
  581. % \begin{macro}{\verbatim@finish}
  582. %    As a default, |\verbatim@finish| processes the remaining
  583. %    characters.
  584. %    When this macro is called we are facing the following problem:
  585. %    when the |\end{verbatim}|
  586. %    command is encountered |\verbatim@processline| is called
  587. %    to process the characters preceding the command on the same
  588. %    line.  If there are none, an empty line would be output if we
  589. %    did not check for this case.
  590. %
  591. %    If the line is empty |\the\verbatim@line| expands to
  592. %    nothing.  To test this we use a trick similar to that on p.\ 376
  593. %    of the \TeX{}book, but with |$|\ldots|$| instead of
  594. %    the |!| tokens.  These tokens can never have the same
  595. %    category code as those appearing in the token register
  596. %    |\verbatim@line| where |$| characters were read with
  597. %    category code $12$ (other).
  598. %    Note that |\ifcat| expands the following tokens so that
  599. %    |\the\verbatim@line| is replaced by the accumulated
  600. %    characters
  601. % \changes{v1.2d}{89/11/29}{Changed `ifx to `ifcat test.}
  602. % \changes{v1.1b}{89/10/18}{Corrected bug in if test (found by Chris).}
  603. %    \begin{macrocode}
  604. \def\verbatim@finish{\ifcat$\the\verbatim@line$\else
  605.   \verbatim@processline\fi}
  606. %    \end{macrocode}
  607. % \end{macro}
  608. %
  609. %
  610. % \subsection{The {\tt verbatim} and {\tt verbatim*} environments}
  611. %
  612. % \begin{macro}{\verbatim@font}
  613. % \changes{v1.2f}{90/01/31}{`@lquote macro removed.}
  614. % \changes{v1.1b}{89/10/18}{`@noligs removed.  Code inserted
  615. %                           directly into `verbatim@font.}
  616. % \changes{v1.1a}{89/10/16}{`verbatim@font added.}
  617. % \changes{v1.1a}{89/10/16}{`@noligs added.}
  618. % \changes{v1.1a}{89/10/16}{`@lquote added.}
  619. %    We start by defining the macro |\verbatim@font| that is
  620. %    to select the font and to set font-dependent parameters.
  621. %    For the default computer modern
  622. %    typewriter font ({\tt cmtt}) we have to avoid the
  623. %    ligatures !` and ?` (as produced by |!`| and |?`|).
  624. %    We do this by making the backquote |`| character active
  625. %    and defining it to insert an
  626. %    explicit kern before the backquote character.
  627. %    If the backquote character is the first character of the
  628. %    a {\tt verbatim} environment, \TeX{} is still in vertical
  629. %    mode.
  630. %    This means that the |\kern| command is processed in vertical mode
  631. %    and treated as a vertical kern.
  632. %    To avoid this, we use the |\leavevmode| macro.
  633. %    While the backquote character is active we cannot use it in a
  634. %    construction like
  635. %    |\catcode`|\meta{char}|=|\meta{number}.
  636. %    Instead we use the ASCII code of this character ($96$).
  637. % \changes{v1.2f}{90/01/31}{`@lquote macro removed.}
  638. % \changes{v1.4c}{90/10/18}{Added `leavevmode.}
  639. %    \begin{macrocode}
  640. \begingroup
  641.  \catcode`\`=\active
  642.  \gdef\verbatim@font{\tt \catcode96\active
  643.    \def`{\leavevmode\kern\z@\char96 }}
  644. \endgroup
  645. %    \end{macrocode}
  646. % \end{macro}
  647. %
  648. %
  649. % \begin{macro}{\@verbatim}
  650. % \changes{v1.1a}{89/10/16}{`@verbatim added.}
  651. %    The macro |\@verbatim| sets up things properly.
  652. %    First of all, the tokens of the |\every@verbatim| hook
  653. %    are inserted.
  654. %    Then a {\tt trivlist} environment is started and its first
  655. %    |\item| command inserted.
  656. %    Each line of the {\tt verbatim} or {\tt verbatim*}
  657. %    environment will be treated as a separate paragraph.
  658. % \changes{v1.2e}{90/01/15}{Added `every@verbatim hook.}
  659. %    \begin{macrocode}
  660. \def\@verbatim{\the\every@verbatim
  661.   \trivlist \item[]%
  662. %    \end{macrocode}
  663. % \changes{v1.3c}{90/02/26}{Removed extra vertical space.
  664. %           Suggested by Frank Mittelbach.}
  665. %    The paragraph parameters are set appropriately:
  666. %    left and right margins, paragraph indentation, the glue to
  667. %    fill the last line and the vertical space between paragraphs.
  668. %    This has to be zero since we do not want to add extra space
  669. %    between lines.
  670. %    \begin{macrocode}
  671.   \leftskip\@totalleftmargin\rightskip\z@
  672.   \parindent\z@\parfillskip\@flushglue\parskip\z@
  673. %    \end{macrocode}
  674. % \changes{v1.1b}{89/10/18}{Added resetting of `parshape
  675. %                           if at beginning of a list.
  676. %                           (Problem pointed out by Chris.)}
  677. %    There's one point to make here:
  678. %    the {\tt list} environment uses \TeX's |\parshape|
  679. %    primitive to get a special indentation for the first line
  680. %    of the  list.
  681. %    If the list begins with a {\tt verbatim} environment
  682. %    this |\parshape| is still in effect.
  683. %    Therefore we have to reset this internal parameter explicitly.
  684. %    We could do this by assigning $0$ to |\parshape|.
  685. %    However, there is a simpler way to achieve this:
  686. %    we simply tell \TeX{} to start a new paragraph.
  687. %    As is explained on p.~103 of the \TeX{}book, this resets
  688. %    |\parshape| to zero.
  689. % \changes{v1.1c}{89/10/19}{Replaced explicit resetting of `parshape
  690. %                           by `@@par.}
  691. %    \begin{macrocode}
  692.   \@@par
  693. %    \end{macrocode}
  694. %    We now ensure that |\par| has the correct definition,
  695. %    namely to force \TeX{} into horizontal mode
  696. %    and to include an empty box.
  697. %    This is to ensure that empty lines do appear in the output.
  698. %    Afterwards, we insert the |\interlinepenalty| since \TeX{}
  699. %    does not add a penalty between paragraphs (here: lines)
  700. %    by its own initiative. Otherwise a |verbatim| environment
  701. %    could be broken across pages even if a |\samepage|
  702. %    declaration were present.
  703. % \changes{v1.4d}{91/04/24}{`penalty`interlinepenalty added.
  704. %                           Necessary to avoid page breaks in
  705. %                           the scope of a `samepage declaration.}
  706. % \changes{v1.2c}{89/10/31}{Definition of `par added.
  707. %                           Ensures identical behaviour for
  708. %                           verbatim and `verbatiminput.
  709. %                           Problem pointed out by Chris.}
  710. %    \begin{macrocode}
  711.   \def\par{\leavevmode\null\@@par\penalty\interlinepenalty}%
  712. %    \end{macrocode}
  713. %    Now we call |\obeylines| to make the end of line character
  714. %    active,
  715. %    \begin{macrocode}
  716.   \obeylines
  717. %    \end{macrocode}
  718. %    switch to the font to be used,
  719. %    \begin{macrocode}
  720.   \verbatim@font
  721. %    \end{macrocode}
  722. %    and change the category code of all special characters
  723. %    to $12$ (other).
  724. %    \begin{macrocode}
  725.   \let\do\@makeother \dospecials}
  726. %    \end{macrocode}
  727. % \end{macro}
  728. %
  729. %
  730. % \begin{macro}{\verbatim}
  731. % \begin{macro}{\verbatim*}
  732. %    Now we define the toplevel macros.
  733. %    |\verbatim| is slightly changed:
  734. %    after setting up things properly it calls
  735. %    |\verbatim@start|.
  736. %    \begin{macrocode}
  737. \def\verbatim{\@verbatim \frenchspacing\@vobeyspaces\verbatim@start}
  738. %    \end{macrocode}
  739. %    |\verbatim*| is defined accordingly.
  740. %    \begin{macrocode}
  741. \@namedef{verbatim*}{\@verbatim\verbatim@start}
  742. %    \end{macrocode}
  743. % \end{macro}
  744. % \end{macro}
  745. %
  746. % \begin{macro}{\endverbatim}
  747. % \begin{macro}{\endverbatim*}
  748. %    To end the {\tt verbatim} and {\tt verbatim*}
  749. %    environments it is only necessary to finish the
  750. %    {\tt trivlist} environment started in |\@verbatim|.
  751. %    \begin{macrocode}
  752. \let\endverbatim=\endtrivlist
  753. \expandafter\let\csname endverbatim*\endcsname =\endtrivlist
  754. %    \end{macrocode}
  755. % \end{macro}
  756. % \end{macro}
  757. %
  758. %
  759. % \subsection{The {\tt comment} environment}
  760. %
  761. % \begin{macro}{\comment}
  762. % \begin{macro}{\endcomment}
  763. % \changes{v1.1c}{89/10/19}{Added `@bsphack/`@esphack to the {\tt
  764. %              comment} environment.  Suggested by Chris Rowley.}
  765. %    The |\comment| macro is similar to |\verbatim*|.
  766. %    However, we do not need to switch fonts or set special
  767. %    formatting parameters such as |\parindent| or |\parskip|.
  768. %    We need only set the category code of all special characters
  769. %    to $12$ (other) and that of |^^M| (the end of line character)
  770. %    to $13$ (active).
  771. %    The latter is needed for macro parameter delimiter matching in
  772. %    the internal macros defined below.
  773. %    In contrast to the default definitions used by the
  774. %    |\verbatim| and |\verbatim*| macros,
  775. %    we define |\verbatim@addtoline| to throw away its argument
  776. %    and |\verbatim@processline|, |\verbatim@startline|,
  777. %    and |\verbatim@finish| to act as no-ops.
  778. %    Then we call |\verbatim@|.
  779. %    But the first thing we do is to call |\@bsphack| so that
  780. %    this environment has no influence whatsoever upon the spacing.
  781. % \changes{v1.1c}{89/10/19}{Changed `verbatim@start to
  782. %                           `verbatim@.  Suggested by Chris Rowley.}
  783. % \changes{v1.1c}{89/10/19}{`verbatim@startline and
  784. %                           `verbatim@finish are now
  785. %                           also redefined to do nothing.}
  786. %    \begin{macrocode}
  787. \def\comment{\@bsphack
  788.              \let\do\@makeother\dospecials\catcode`\^^M\active
  789.              \let\verbatim@startline\relax
  790.              \let\verbatim@addtoline\@gobble
  791.              \let\verbatim@processline\relax
  792.              \let\verbatim@finish\relax
  793.              \verbatim@}
  794. %    \end{macrocode}
  795. %    |\endcomment| is very simple: it only calls
  796. %    |\@esphack| to take care of the spacing.
  797. %    The |\end| macro closes the group and therefore takes care
  798. %    of restoring everything we changed.
  799. %    \begin{macrocode}
  800. \let\endcomment=\@esphack
  801. %    \end{macrocode}
  802. % \end{macro}
  803. % \end{macro}
  804. %
  805. %
  806. %
  807. % \subsection{The main loop}
  808. %
  809. % Here comes the tricky part:
  810. % During the definition of the macros we need to use the special
  811. % characters |\|, |{|, and |}| not only with their
  812. % normal category codes,
  813. % but also with category code $12$ (other).
  814. % We achieve this by the following trick:
  815. % first we tell \TeX{} that |\|, |{|, and |}|
  816. % are the lowercase versions of |!|, |[|, and |]|.
  817. % Then we replace every occurrence of |\|, |{|, and |}|
  818. % that should be read with category code $12$ by |!|, |[|,
  819. % and |]|, respectively,
  820. % and give the whole list of tokens to |\lowercase|,
  821. % knowing that category codes are not altered by this primitive!
  822. %
  823. % But first we have ensure that
  824. % |!|, |[|, and |]| themselves have
  825. % the correct category code!
  826. % \changes{v1.3b}{90/02/07}{Introduced `vrb@catcodes instead
  827. %                  of explicit setting of category codes.}
  828. % To allow special settings of these codes we hide their setting in
  829. % the macro |\vrb@catcodes|.  If it is already defined our new
  830. % definition is skipped.
  831. %    \begin{macrocode}
  832. \@ifundefined{vrb@catcodes}%
  833.   {\def\vrb@catcodes{%
  834.      \catcode`\!12\catcode`\[12\catcode`\]12}}{}
  835. %    \end{macrocode}
  836. % This allows the use of this code for applications where other
  837. % category codes are in effect.
  838. %
  839. % We start a group to keep the category code changes local.
  840. %    \begin{macrocode}
  841. \begingroup
  842.  \vrb@catcodes
  843.  \lccode`\!=`\\ \lccode`\[=`\{ \lccode`\]=`\}
  844. %    \end{macrocode}
  845. % \changes{v1.2f}{90/01/31}{Code for TABs removed.}
  846. %    We also need the end-of-line character |^^M|,
  847. %    as an active character.
  848. %    If we were to simply write |\catcode`\^^M=\active|
  849. %    then we would get an unwanted active end of line character
  850. %    at the end of every line of the following macro definitions.
  851. %    Therefore we use the same trick as above:
  852. %    we write a tilde |~| instead of |^^M| and
  853. %    pretend that the
  854. %    latter is the lowercase variant of the former.
  855. %    Thus we have to ensure now that the tilde character has
  856. %    category code $13$ (active).
  857. %    \begin{macrocode}
  858.  \catcode`\~=\active \lccode`\~=`\^^M
  859. %    \end{macrocode}
  860. %    The use of the |\lowercase| primitive leads to one problem:
  861. %    the uppercase character `|C|' needs to be used in the
  862. %    code below and its case must be preserved.
  863. %    So we add the command:
  864. %    \begin{macrocode}
  865.  \lccode`\C=`\C
  866. %    \end{macrocode}
  867. %    Now we start the token list passed to |\lowercase|.
  868. %    \begin{macrocode}
  869.  \lowercase{%
  870. %    \end{macrocode}
  871. % Since this is done in a group all macro definitions are executed
  872. % globally.
  873. % \begin{macro}{\verbatim@start}
  874. %    The purpose of |\verbatim@start| is to check whether there
  875. %    are any characters on the same line as the |\begin{verbatim}|
  876. %    and to pretend that they were on a line by themselves.
  877. %    On the other hand, if there are no characters remaining
  878. %    on the current line we shall just find an end of line character.
  879. %    |\verbatim@start| performs its task by first grabbing the
  880. %    following character (its argument).
  881. %    This argument is then compared to an active |^^M|,
  882. %    the end of line character.
  883. %    \begin{macrocode}
  884.     \gdef\verbatim@start#1{%
  885.       \verbatim@startline
  886.       \if\noexpand#1\noexpand~%
  887. %    \end{macrocode}
  888. %    If this is true we transfer control to |\verbatim@|
  889. %    to process the next line.  We use
  890. %    |\next| as the macro which will continue the work.
  891. %    \begin{macrocode}
  892.         \let\next\verbatim@
  893. %    \end{macrocode}
  894. %    Otherwise, we define |\next| to expand to a call
  895. %    to |\verbatim@| followed by the character just
  896. %    read so that it is reinserted into the text.
  897. %    This means that those characters remaining on this line
  898. %    are handled as if they formed a line by themselves.
  899. %    \begin{macrocode}
  900.       \else \def\next{\verbatim@#1}\fi
  901. %    \end{macrocode}
  902. %    Finally we call |\next|.
  903. %    \begin{macrocode}
  904.       \next}%
  905. %    \end{macrocode}
  906. % \end{macro}
  907. %
  908. % \begin{macro}{\verbatim@}
  909. %    The three macros |\verbatim@|, |\verbatim@@|,
  910. %    and |\verbatim@@@| form the ``main loop'' of the
  911. %    {\tt verbatim} environment.
  912. %    The purpose of |\verbatim@| is to read exactly one line
  913. %    of input.
  914. %    |\verbatim@@| and |\verbatim@@@| work together to
  915. %    find out whether the four characters
  916. %    |\end| (all with category code $12$ (other)) occur in that
  917. %    line.
  918. %    If so, |\verbatim@@@| will call
  919. %    |\verbatim@test| to check whether this |\end| is
  920. %    part of |\end{verbatim}| and will terminate the environment
  921. %    if this is the case.
  922. %    Otherwise we continue as if nothing had happened.
  923. %    So let's have a look at the definition of |\verbatim@|:
  924. % \changes{v1.1a}{89/10/16}{Replaced `verbatim@@@ by `@nil.}
  925. %    \begin{macrocode}
  926.     \gdef\verbatim@#1~{\verbatim@@#1!end\@nil}%
  927. %    \end{macrocode}
  928. %    Note that the |!| character will have been replaced by a
  929. %    |\| with category code $12$ (other) by the |\lowercase|
  930. %    primitive governing this code before the definition of this
  931. %    macro actually takes place.
  932. %    That means that
  933. %    it takes the line, puts |\end| (four character tokens)
  934. %    and |\@nil| (one control sequence token) as a
  935. %    delimiter behind it, and
  936. %    then calls |\verbatim@@|.
  937. % \end{macro}
  938. %
  939. % \begin{macro}{\verbatim@@}
  940. %    |\verbatim@@| takes everything up to the next occurrence of
  941. %    the four characters |\end| as its argument.
  942. %    \begin{macrocode}
  943.     \gdef\verbatim@@#1!end{%
  944. %    \end{macrocode}
  945. %    That means: if they do not occur in the original line, then
  946. %    argument |#1| is the
  947. %    whole input line, and |\@nil| is the next token
  948. %    to be processed.
  949. %    However, if the four characters |\end| are part of the
  950. %    original line, then
  951. %    |#1| consists of the characters in front of |\end|,
  952. %    and the next token is the following character (always remember
  953. %    that the line was lengthened by five tokens).
  954. %    Whatever |#1| may be, it is verbatim text,
  955. %    so |#1| is added to the line currently built.
  956. %    \begin{macrocode}
  957.        \verbatim@addtoline{#1}%
  958. %    \end{macrocode}
  959. %    The next token in the input stream
  960. %    is of special interest to us.
  961. %    Therefore |\futurelet| defines |\next| to be equal
  962. %    to it before calling |\verbatim@@@|.
  963. %    \begin{macrocode}
  964.        \futurelet\next\verbatim@@@}%
  965. %    \end{macrocode}
  966. % \end{macro}
  967. %
  968. % \begin{macro}{\verbatim@@@}
  969. % \changes{v1.1a}{89/10/16}{Replaced `verbatim@@@ by
  970. %                           `@nil where used as delimiter.}
  971. %    |\verbatim@@@| will now read the rest of the tokens on
  972. %    the current line,
  973. %    up to the final |\@nil| token.
  974. %    \begin{macrocode}
  975.     \gdef\verbatim@@@#1\@nil{%
  976. %    \end{macrocode}
  977. %    If the first of the above two cases occurred, i.e.\ no
  978. %    |\end| characters were on that line, |#1| is empty
  979. %    and |\next| is equal to |\@nil|.
  980. %    This is easily checked.
  981. %    \begin{macrocode}
  982.        \ifx\next\@nil
  983. %    \end{macrocode}
  984. %    If so, this was a simple line.
  985. %    We finish it by processing the line we accumulated so far.
  986. %    Then we prepare to read the next line.
  987. % \changes{v1.2f}{90/01/31}{Added `verbatim@startline.}
  988. %    \begin{macrocode}
  989.          \verbatim@processline
  990.          \verbatim@startline
  991.          \let\next\verbatim@
  992. %    \end{macrocode}
  993. %    Otherwise we have to check what follows these |\end|
  994. %    tokens.
  995. %    \begin{macrocode}
  996.        \else
  997. %    \end{macrocode}
  998. %    Before we continue, it's a good idea to stop for a moment
  999. %    and remember where we are:
  1000. %    We have just read the four character tokens |\end|
  1001. %    and must now check whether the name of the environment (surrounded
  1002. %    by braces) follows.
  1003. %    To this end we define a macro called |\@tempa|
  1004. %    that reads exactly one character and decides what to do next.
  1005. %    This macro should do the following: skip spaces until
  1006. %    it encounters either a left brace or the end of the line.
  1007. %    But it is important to remember which characters are skipped.
  1008. %    The |\end|\meta{optional spaces}|{| characters
  1009. %    may be part of the verbatim text, i.e.\ these characters
  1010. %    must be printed.
  1011. %
  1012. %    Assume for example that the current line contains
  1013. %    \begin{verbatim*}
  1014. %      \end {AVeryLongEnvironmentName}
  1015. %\end{verbatim*}
  1016. %    As we shall soon see, the scanning mechanism implemented here
  1017. %    will not find out that this is text to be printed until
  1018. %    it has read the right brace.
  1019. %    Therefore we need a way to accumulate the characters read
  1020. %    so that we can reinsert them if necessary.
  1021. %    The token register |\@temptokena| is used for this purpose.
  1022. %
  1023. %    Before we do this we have to get rid of the superfluous
  1024. %    |\end| tokens at the end of the line.
  1025. %    To this end we define a temporary macro whose argument
  1026. %    is delimited by |\end\@nil| (four character tokens
  1027. %    and one control sequence token)
  1028. %    and use it on the rest of the line,
  1029. %    after appending a |\@nil| token to it.
  1030. %    This token can never appear in |#1|.
  1031. %    We use the following definition of
  1032. %    |\@tempa| to store the rest of the line (after the first
  1033. %    |\end|) in token register |\toks@|
  1034. %    which we shall use again in a moment.
  1035. %    \begin{macrocode}
  1036.          \def\@tempa##1!end\@nil{\toks@{##1}}%
  1037.            \@tempa#1\@nil
  1038. %    \end{macrocode}
  1039. %    We mentioned already that we use token register
  1040. %    |\@temptokena|
  1041. %    to remember the characters we skip, in case we need them again.
  1042. %    We initialize this with the |\end| we have thrown away
  1043. %    in the call to |\@tempa|.
  1044. %    \begin{macrocode}
  1045.          \@temptokena{!end}%
  1046. %    \end{macrocode}
  1047. %    We shall now call |\verbatim@test|
  1048. %    to process the characters
  1049. %    remaining on the current line.
  1050. %    But wait a moment: we cannot simply call this macro
  1051. %    since we have already read the whole line.
  1052. %    We stored its characters in token register |\toks@|.
  1053. %    Therefore we use the following |\edef| to insert them again
  1054. %    after the |\verbatim@test| token.
  1055. %    A |^^M| character is appended to denote the end of the line.
  1056. % \changes{v1.2}{89/10/20}{Taken local definition of `@tempa out of
  1057. %                          `verbatim@@@ and introduced
  1058. %                          `verbatim@test instead.}
  1059. %    \begin{macrocode}
  1060.          \edef\next{\noexpand\verbatim@test\the\toks@\noexpand~}%
  1061. %    \end{macrocode}
  1062. %    That's almost all, but we still have to
  1063. %    now call |\next| to do the work.
  1064. %    \begin{macrocode}
  1065.        \fi \next}%
  1066. %    \end{macrocode}
  1067. % \end{macro}
  1068. %
  1069. %
  1070. % \begin{macro}{\verbatim@test}
  1071. % \changes{v1.2}{89/10/20}{Introduced `verbatim@test.}
  1072. %    We define |\verbatim@test| to investigate every token
  1073. %    in turn.
  1074. %    \begin{macrocode}
  1075.  \gdef\verbatim@test#1{%
  1076. %    \end{macrocode}
  1077. %    First of all we set |\next| equal to |\verbatim@test|
  1078. %    in case this macro must call itself recursively in order to
  1079. %    skip spaces.
  1080. %    \begin{macrocode}
  1081.            \let\next\verbatim@test
  1082. %    \end{macrocode}
  1083. %    We have to distinguish four cases:
  1084. %    \begin{enumerate}
  1085. %      \item The next token is a |^^M|, i.e.\ we reached
  1086. %            the end of the line.  That means that nothing
  1087. %            special was found.
  1088. %            Note that we use |\if| for the following
  1089. %            comparisons so that the category code of the
  1090. %            characters is irrelevant.
  1091. %    \begin{macrocode}
  1092.            \if\noexpand#1\noexpand~%
  1093. %    \end{macrocode}
  1094. %            We add the characters accumulated in token register
  1095. %            |\@temptokena| to the current line.  Since
  1096. %            |\verbatim@addtoline| does not expand its argument,
  1097. %            we have to do the expansion at this point.  Then we
  1098. %            |\let| |\next| equal to |\verbatim@|
  1099. %            to prepare to read the next line.
  1100. % \changes{v1.2f}{90/01/31}{Added `verbatim@startline.}
  1101. %    \begin{macrocode}
  1102.              \expandafter\verbatim@addtoline
  1103.                \expandafter{\the\@temptokena}%
  1104.              \verbatim@processline
  1105.              \verbatim@startline
  1106.              \let\next\verbatim@
  1107. %    \end{macrocode}
  1108. %      \item A space character follows.
  1109. %            This is allowed, so we add it to |\@temptokena|
  1110. %            and continue.
  1111. %    \begin{macrocode}
  1112.            \else \if\noexpand#1
  1113.              \@temptokena\expandafter{\the\@temptokena#1}%
  1114. %    \end{macrocode}
  1115. % \changes{v1.2f}{90/01/31}{Code for TABs removed.}
  1116. %      \item An open brace follows.
  1117. %            This is the most interesting case.
  1118. %            We must now collect characters until we read the closing
  1119. %            brace and check whether they form the environment name.
  1120. %            This will be done by |\verbatim@testend|, so here
  1121. %            we let |\next| equal this macro.
  1122. %            Again we will process the rest of the line, character
  1123. %            by character.
  1124. % \changes{v1.2}{89/10/20}{Moved the initialization of
  1125. %                          `@tempc from `verbatim@testend into
  1126. %                          `verbatim@test.}
  1127. %            The characters forming the name of the environment will
  1128. %            be accumulated in |\@tempc|.
  1129. %            We initialize this macro to expand to nothing.
  1130. % \changes{v1.3b}{90/02/07}{`noexpand added.}
  1131. %    \begin{macrocode}
  1132.            \else \if\noexpand#1\noexpand[%
  1133.              \let\@tempc\@empty
  1134.              \let\next\verbatim@testend
  1135. %    \end{macrocode}
  1136. %            Note that the |[| character will be a |{| when
  1137. %            this macro is defined.
  1138. %      \item Any other character means that the |\end| was part
  1139. %            of the verbatim text.
  1140. %            Add the characters to the current line and prepare to call
  1141. %            |\verbatim@| to process the rest of the line.
  1142. %  \changes{v1.0f}{89/10/09}{Fixed `end `end bug
  1143. %                            found by Chris Rowley}
  1144. %    \begin{macrocode}
  1145.            \else
  1146.              \expandafter\verbatim@addtoline
  1147.                \expandafter{\the\@temptokena}%
  1148.              \def\next{\verbatim@#1}%
  1149.            \fi\fi\fi
  1150. %    \end{macrocode}
  1151. %    \end{enumerate}
  1152. %    The last thing this macro does is to call |\next|
  1153. %    to continue processing.
  1154. %    \begin{macrocode}
  1155.            \next}%
  1156. %    \end{macrocode}
  1157. % \end{macro}
  1158. %
  1159. % \begin{macro}{\verbatim@testend}
  1160. %    |\verbatim@testend| is called when
  1161. %    |\end|\meta{optional spaces}|{| was seen.
  1162. %    Its task is to scan everything up to the next |}|
  1163. %    and to call |\verbatim@@testend|.
  1164. %    If no |}| is found it must reinsert the characters it read
  1165. %    and return to |\verbatim@|.
  1166. %    The following definition is similar to that of
  1167. %    |\verbatim@test|:
  1168. %    it takes the next character and decides what to do.
  1169. % \changes{v1.2}{89/10/20}{Removed local definition of `@tempa from
  1170. %                          `verbatim@testend which now
  1171. %                          does the work itself.}
  1172. %    \begin{macrocode}
  1173.     \gdef\verbatim@testend#1{%
  1174. %    \end{macrocode}
  1175. %    Again, we have four cases:
  1176. %    \begin{enumerate}
  1177. %      \item |^^M|: As no |}| is found in the current line,
  1178. %            add the characters to the buffer.  To avoid a
  1179. %            complicated construction for expanding
  1180. %            |\@temptokena|
  1181. %            and |\@tempc| we do it in two steps.  Then we
  1182. %            continue with |\verbatim@| to process the
  1183. %            next line.
  1184. % \changes{v1.2f}{90/01/31}{Added `verbatim@startline.}
  1185. %    \begin{macrocode}
  1186.          \if\noexpand#1\noexpand~%
  1187.            \expandafter\verbatim@addtoline
  1188.              \expandafter{\the\@temptokena[}%
  1189.            \expandafter\verbatim@addtoline
  1190.              \expandafter{\@tempc}%
  1191.            \verbatim@processline
  1192.            \verbatim@startline
  1193.            \let\next\verbatim@
  1194. %    \end{macrocode}
  1195. %      \item |}|: Call |\verbatim@@testend| to check
  1196. %            if this is the right environment name.
  1197. % \changes{v1.3b}{90/02/07}{`noexpand added.}
  1198. %    \begin{macrocode}
  1199.          \else\if\noexpand#1\noexpand]%
  1200.            \let\next\verbatim@@testend
  1201. %    \end{macrocode}
  1202. %  \changes{v1.0f}{89/10/09}{Introduced check for {\tt\bslash} to fix
  1203. %                            single brace bug found by Chris Rowley}
  1204. %      \item |\|: This character must not occur in the name of
  1205. %            an environment.  Thus we stop collecting characters.
  1206. %            In principle, the same argument would apply to other
  1207. %            characters as well, e.g., |{|.
  1208. %            However, |\| is a special case, since it may be
  1209. %            the first character of |\end|.  This means that
  1210. %            we have to look again for
  1211. %            |\end{|\meta{environment name}|}|.
  1212. %            Note that we prefixed the |!| by a |\noexpand|
  1213. %            primitive, to protect ourselves against it being an
  1214. %            active character.
  1215. % \changes{v1.3b}{90/02/07}{`noexpand added.}
  1216. %    \begin{macrocode}
  1217.          \else\if\noexpand#1\noexpand!%
  1218.            \expandafter\verbatim@addtoline
  1219.              \expandafter{\the\@temptokena[}%
  1220.            \expandafter\verbatim@addtoline
  1221.              \expandafter{\@tempc}%
  1222.            \def\next{\verbatim@!}%
  1223. %    \end{macrocode}
  1224. %      \item Any other character: collect it and continue.
  1225. %            We cannot use |\edef| to define |\@tempc|
  1226. %            since its replacement text might contain active
  1227. %            character tokens.
  1228. %    \begin{macrocode}
  1229.          \else \expandafter\def\expandafter\@tempc\expandafter
  1230.            {\@tempc#1}\fi\fi\fi
  1231. %    \end{macrocode}
  1232. %    \end{enumerate}
  1233. %    As before, the macro ends by calling itself, to
  1234. %    process the next character if appropriate.
  1235. %    \begin{macrocode}
  1236.          \next}%
  1237. %    \end{macrocode}
  1238. % \end{macro}
  1239. %
  1240. % \begin{macro}{\verbatim@@testend}
  1241. %    Unlike the previous macros |\verbatim@@testend| is simple:
  1242. %    it has only to check if the |\end{|\ldots|}|
  1243. %    matches the corresponding |\begin{|\ldots|}|.
  1244. %    \begin{macrocode}
  1245.     \gdef\verbatim@@testend{%
  1246. %    \end{macrocode}
  1247. %    We use |\next| again to define the things that are
  1248. %    to be done.
  1249. %    Remember that the name of the current environment is
  1250. %    held in |\@currenvir|, the characters accumulated
  1251. %    by |\verbatim@testend| are in |\@tempc|.
  1252. %    So we simply compare these and prepare to execute
  1253. %    |\end{|\meta{current environment}|}|
  1254. %    macro if they match.
  1255. %    Before we do this we call |\verbatim@finish| to process
  1256. %    the last line.
  1257. %    We define |\next| via |\edef| so that
  1258. %    |\@currenvir| is replaced by its expansion.
  1259. %    Therefore we need |\noexpand| to inhibit the expansion
  1260. %    of |\end| at this point.
  1261. %    \begin{macrocode}
  1262.        \ifx\@tempc\@currenvir
  1263.          \verbatim@finish
  1264.          \edef\next{\noexpand\end{\@currenvir}%
  1265. %    \end{macrocode}
  1266. %    Without this trick the |\end| command would not be able
  1267. %    to correctly check whether its argument matches the name of
  1268. %    the current environment and you'd get an
  1269. %    interesting \LaTeX{} error message such as:
  1270. %    \begin{verbatim}
  1271. %! \begin{verbatim*} ended by \end{verbatim*}.
  1272. %\end{verbatim}
  1273. %    But what do we do with the rest of the characters, those
  1274. %    that remain on that line?
  1275. %    We call |\verbatim@rescan| to take care of that.
  1276. %    Its first argument is the name of the environment just
  1277. %    ended, in case we need it again.
  1278. %    |\verbatim@rescan| takes the list of characters to be
  1279. %    reprocessed as its second argument.
  1280. %    (This token list was inserted after the current macro
  1281. %    by |\verbatim@@@|.)
  1282. %    Since we are still in an |\edef| we protect it
  1283. %    by means of|\noexpand|.
  1284. %    \begin{macrocode}
  1285.                     \noexpand\verbatim@rescan{\@currenvir}}%
  1286. %    \end{macrocode}
  1287. %    If the names do not match, we reinsert everything read up
  1288. %    to now and prepare to call |\verbatim@| to process
  1289. %    the rest of the line.
  1290. %    \begin{macrocode}
  1291.        \else
  1292.          \expandafter\verbatim@addtoline
  1293.            \expandafter{\the\@temptokena[}%
  1294.            \expandafter\verbatim@addtoline
  1295.              \expandafter{\@tempc]}%
  1296.          \let\next\verbatim@
  1297.        \fi
  1298. %    \end{macrocode}
  1299. %    Finally we call |\next|.
  1300. %    \begin{macrocode}
  1301.        \next}%
  1302. %    \end{macrocode}
  1303. % \end{macro}
  1304. %
  1305. % \begin{macro}{\verbatim@rescan}
  1306. %    In principle |\verbatim@rescan| could be used to
  1307. %    analyse the characters remaining after the |\end{...}|
  1308. %    command and pretend that these were read
  1309. %    ``properly'', assuming ``standard'' category codes are in
  1310. %    force.\footnote{Remember that they were all read with
  1311. %          category codes $11$ (letter) and $12$ (other) so
  1312. %          that control sequences are not recognized as such.}
  1313. %    But this is not always possible (when there are unmatched
  1314. %    curly braces in the rest of the line).
  1315. %    Besides, we think that this is not worth the effort:
  1316. %    After a {\tt verbatim} or {\tt verbatim*} environment
  1317. %    a new line in the output is begun anyway,
  1318. %    and an |\end{comment}| can easily be put on a line by itself.
  1319. %    So there is no reason why there should be any text here.
  1320. %    For the benefit of the user who did put something there
  1321. %    (a comment, perhaps)
  1322. %    we simply issue a warning and drop them.
  1323. %    The method of testing is explained in Appendix~D, p.\ 376 of
  1324. %    the \TeX{}book. We use |^^M| instead of the |!|
  1325. %    character used there
  1326. %    since this is a character that cannot appear in |#1|.
  1327. %    The two |\noexpand| primitives are necessary to avoid
  1328. %    expansion of active characters and macros.
  1329. %
  1330. %    One extra subtlety should be noted here: remember that
  1331. %    the token list we are currently building will first be
  1332. %    processed by the |\lowercase| primitive before \TeX{}
  1333. %    carries out the definitions.
  1334. %    This means that the `|C|' character in the
  1335. %    argument to the |\@warning| macro must be protected against
  1336. %    being changed to `|c|'.  That's the reason why we added the
  1337. %    |\lccode`\C=`\C| assignment above.
  1338. %    We can now finish the argument to |\lowercase| as well as the
  1339. %    group in which the category codes were changed.
  1340. %    \begin{macrocode}
  1341.   \gdef\verbatim@rescan#1#2~{\if\noexpand~\noexpand#2~\else
  1342.         \@warning{Characters dropped after `\string\end{#1}'}\fi}}
  1343. \endgroup
  1344. %    \end{macrocode}
  1345. % \end{macro}
  1346. %
  1347. %
  1348. % \subsection{The {\tt\bslash verbatiminput} command}
  1349. %
  1350. % \begin{macro}{\verbatiminput}
  1351. %    |\verbatiminput| first starts a group
  1352. %    to keep font and category changes local.
  1353. %    \begin{macrocode}
  1354. \def\verbatiminput{\begingroup
  1355. %    \end{macrocode}
  1356. %    The right sequence of actions is crucial here.
  1357. %    First we must check if a star follows.
  1358. %    Then we must read the argument (the file name).
  1359. %    Finally we must set up everything to read the contents of the
  1360. %    file verbatim.
  1361. %    Therefore we must not start by calling |\@verbatim| to change
  1362. %    font and the category code of characters.
  1363. %    Instead we call one of the macros |\sverbatim@input| or
  1364. %    |\verbatim@input|, depending on whether a star follows.
  1365. %    \begin{macrocode}
  1366.   \@ifstar\sverbatim@input\verbatim@input}
  1367. %    \end{macrocode}
  1368. % \end{macro}
  1369. %
  1370. %
  1371. % \begin{macro}{\sverbatim@input}
  1372. %    |\sverbatim@input| reads the file name argument and sets up
  1373. %    everything as in the |\verbatim| macro.
  1374. %    Then it reads in the file, finishes off the {\tt trivlist}
  1375. %    environment started by |\@verbatim| and closes the group
  1376. %    opened in |\verbatiminput|.
  1377. %    This restores everything to its normal settings.
  1378. % \changes{v1.2f}{90/01/31}{Added `@doendpe.  Found by Erica Harris.}
  1379. % \changes{v1.1c}{89/10/19}{Forgotten `endtrivlist added.
  1380. %                           Found by Chris Rowley.}
  1381. %    \begin{macrocode}
  1382. \def\sverbatim@input#1{\@verbatim
  1383.   \@input{#1}\endtrivlist\endgroup\@doendpe}
  1384. %    \end{macrocode}
  1385. % \end{macro}
  1386. %
  1387. % \begin{macro}{\verbatim@input}
  1388. %    |\verbatim@input| is nearly the same; it additionally calls
  1389. %    |\frenchspacing| and |\@vobeyspaces| (as in
  1390. %    |\verbatim| and |\verb|).
  1391. % \changes{v1.2f}{90/01/31}{Added `@doendpe.  Found by Erica Harris.}
  1392. % \changes{v1.1c}{89/10/19}{Forgotten `endtrivlist added.
  1393. %                           Found by Chris Rowley.}
  1394. %    \begin{macrocode}
  1395. \def\verbatim@input#1{\@verbatim
  1396.   \frenchspacing \@vobeyspaces
  1397.   \@input{#1}\endtrivlist\endgroup\@doendpe}
  1398. %    \end{macrocode}
  1399. % \end{macro}
  1400. %
  1401. %
  1402. % \subsection{Redefinition of the {\tt \bslash verb} command.}
  1403. %
  1404. % The implementation here has the following advantage over that in
  1405. % the original \LaTeX: it will not accept the end of the input
  1406. % line being reached before the verbatim text has ended.  Instead, it
  1407. % will end the verbatim text and generate an error message.
  1408. %
  1409. % \begin{macro}{\verb}
  1410. % \changes{v1.1a}{89/10/16}{`verb added.}
  1411. %    We need special category codes during the definition:
  1412. %    the end of line character (|^^M|) must be an active
  1413. %    character.  We do this in the same way as above:
  1414. % \changes{v1.4g}{91/11/21}{Changed `verb so that it works in math mode as well.
  1415. %          Suggested by Chris Rowley and Bernd Raichle.}
  1416. %    \begin{macrocode}
  1417. \begingroup
  1418.   \catcode`\~=\active
  1419.   \lccode`\~=`\^^M
  1420. %    \end{macrocode}
  1421. %    We use the following trick proposed by Bernd Raichle:
  1422. %    The very first token in the list given to |\lowercase| is
  1423. %    the |\endgroup| primitive. This means that it is processed by
  1424. %    \TeX{} immediately after |\lowercase| is finished, thus ending
  1425. %    the group started by |\begingroup| above.
  1426. %    \begin{macrocode}
  1427.   \lowercase{\endgroup
  1428. %    \end{macrocode}
  1429. %    The first thing we do in |\verb| is to check for math mode.
  1430. %    If this is the case the |\bgroup| is used as open brace for the
  1431. %    |\hbox|. Otherwise, we force horizontal mode and include an
  1432. %    empty |\hbox| (|\null|). This is necessary in case the first
  1433. %    character to be processed verbatim happens to be a space and
  1434. %    if a line break occurs at this point. If we did not include
  1435. %    the empty |\hbox| this space would disappear.
  1436. %    \begin{macrocode}
  1437.     \gdef\verb{\relax
  1438.       \ifmmode \hbox \else \leavevmode\null \fi
  1439.       \bgroup
  1440. %    \end{macrocode}
  1441. %    We use here
  1442. %    |\verbatim@font| rather than switching directly to
  1443. %    |\tt|.
  1444. %    \begin{macrocode}
  1445.       \verbatim@font
  1446. %    \end{macrocode}
  1447. %    Now we make the end of line character active and define it to
  1448. %    restore everything back to normal and to signal an error.
  1449. %    Remember that every occurence of the |~| character is changed
  1450. %    into the end of line character.
  1451. % \changes{v1.4g}{91/11/21}{Changed control-m to tilde in category
  1452. %                   code assignment, as it is converted to control-m
  1453. %                   by the `lowercase primitive. This improves the
  1454. %                   readability. Suggested by Bernd Raichle.}
  1455. % \changes{v1.4b}{90/07/14}{Added `catcode assignment that had
  1456. %                   disappeared somehow.}
  1457. %    \begin{macrocode}
  1458.       \catcode`~\active
  1459.       \def~{\egroup\@latexerr{\string\verb\space command ended by
  1460.                               end of line.}\@ehc}%
  1461. %    \end{macrocode}
  1462. %    The rest is copied from {\tt latex.tex} where we have replaced
  1463. %    one macro (|\@verb|) by its expansion.
  1464. % \changes{v1.3a}{90/02/04}{Removed unnecessary braces.}
  1465. % \changes{v1.4}{90/03/07}{Replaced `@verb by its expansion.}
  1466. %    \begin{macrocode}
  1467.     \let\do\@makeother \dospecials
  1468.     \@ifstar\@sverb{\@vobeyspaces \frenchspacing \@sverb}}}
  1469. %    \end{macrocode}
  1470. % \end{macro}
  1471. %
  1472. % \begin{macro}{\@sverb}
  1473. % \changes{v1.4h}{91/12/18}{Changed `@sverb so that `verb would work
  1474. %           in a tabular or array environment.}
  1475. % \changes{v1.4i}{92/01/20}{Errors corrected.} 
  1476. %    |\@sverb| gains control when we are ready to look for the
  1477. %    delimiting character.  It reads it and defines this character
  1478. %    to be equivalent to |\egroup|, i.e., to a closing brace.
  1479. %    That means it will restore everything to normal when it occurs for
  1480. %    the second time.
  1481. %    \begin{macrocode}
  1482. \def\@sverb#1{%
  1483.   \catcode`#1\active
  1484.   \lccode`\~`#1%
  1485.   \lowercase{\let~\egroup}}%
  1486. %    \end{macrocode}
  1487. % \end{macro}
  1488. %
  1489. % \Finale
  1490. %
  1491. \endinput
  1492.  
  1493.